<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
     <!-- <el-form-item label="主键ID" prop="teacherId">
        <el-input v-model="dataForm.teacherId" placeholder="主键ID"></el-input>
      </el-form-item>-->
      <el-form-item label="职业阶段名称" prop="proStageId">
        <template>
          <el-select v-model="dataForm.proStageId" placeholder="请选择">
            <el-option
              v-for="item in dataForm.proStageNameList"
              :key="item.proStageId"
              :label="item.proStageName"
              :value="item.proStageId">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input v-model="dataForm.courseName" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item label="课程方向" prop="direction">
        <el-input v-model="dataForm.direction" placeholder="课程方向"></el-input>
      </el-form-item>
      <el-form-item label="课程级别" prop="courseLevel">
        <template>
          <el-select v-model="dataForm.courseLevel" placeholder="请选择">
            <el-option
              v-for="item in dataForm.courseLevelList"
              :key="item.courseLevelId"
              :label="item.courseLevelValue"
              :value="item.courseLevelKey">
            </el-option>
          </el-select>
        </template>
      </el-form-item>
      <el-form-item label="是否免费" prop="courseFree">
        <template>
          <el-radio-group v-model="dataForm.courseFree">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>
      <el-form-item label="课程价格" prop="coursePrice">
        <el-input placeholder="课程价格"   v-model="dataForm.coursePrice">
          <template slot="append">&yen;</template>
        </el-input>
      </el-form-item>
      <el-form-item label="课程描述" prop="remark">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model="dataForm.remark" placeholder="课程描述"></el-input>
      </el-form-item>
      <el-form-item label="学习人数" prop="studyCount">
        <el-input-number v-model="dataForm.studyCount"  :min="0" label="学习人数"></el-input-number>
      </el-form-item>
      <el-form-item label="创建人" prop="createUser">
        <el-input v-model="dataForm.createUser" disabled placeholder="创建人"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="dataForm.createTime" disabled placeholder="创建时间"></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updateUser">
        <el-input v-model="dataForm.updateUser" disabled placeholder="修改人"></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" disabled placeholder="修改时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          courseId: 0,
          teacherId: '',
          proStageId: '',
          courseName: '',
          proStageName: '',
          direction: '',
          courseLevel: '',
          courseFree: 0,
          coursePrice: '',
          remark: '',
          studyCount: '',
          createUser: '',
          createTime: '',
          updateUser: '',
          updateTime: '',
          proStageNameList: '',
          courseLevelList: [
            {courseLevelId: 1, courseLevelKey: 1, courseLevelValue: '初级'},
            {courseLevelId: 2, courseLevelKey: 2, courseLevelValue: '中级'},
            {courseLevelId: 3, courseLevelKey: 3, courseLevelValue: '高级'}
          ]
        },
        dataRule: {
          teacherId: [
            {required: true, message: '主键ID不能为空', trigger: 'blur'}
          ],
          proStageId: [
            {required: true, message: '职业阶段主键ID不能为空', trigger: 'blur'}
          ],
          courseName: [
            {required: true, message: '课程名称不能为空', trigger: 'blur'}
          ],
          direction: [
            {required: true, message: '课程方向不能为空', trigger: 'blur'}
          ],
          courseLevel: [
            {required: true, message: '课程级别：1-初级，2-中级，3-高级不能为空', trigger: 'blur'}
          ],
          courseFree: [
            {required: true, message: '是否免费：0-否，1-是不能为空', trigger: 'blur'}
          ],
          coursePrice: [
            {required: true, message: '课程价格不能为空', trigger: 'blur'}
          ],
          remark: [
            {required: true, message: '课程描述不能为空', trigger: 'blur'}
          ],
          studyCount: [
            {required: true, message: '学习人数不能为空', trigger: 'blur'}
          ]
          /*  createUser: [
           { required: true, message: '创建人不能为空', trigger: 'blur' }
           ],
           createTime: [
           { required: true, message: '创建时间不能为空', trigger: 'blur' }
           ],
           updateUser: [
           { required: true, message: '修改人不能为空', trigger: 'blur' }
           ],
           updateTime: [
           { required: true, message: '修改时间不能为空', trigger: 'blur' }
           ] */
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.courseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // 获取职业阶段名称
          this.$http({
            url: this.$http.adornUrl(`operate/professionstage/totalList`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.proStageId = ''
              this.dataForm.proStageNameList = data.list
            }
          })
          if (this.dataForm.courseId) {
            this.$http({
              url: this.$http.adornUrl(`/teach/course/info/${this.dataForm.courseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teacherId = data.tCourse.teacherId
                this.dataForm.proStageName = data.tCourse.proStageName
                this.dataForm.proStageId = data.tCourse.proStageId
                this.dataForm.courseName = data.tCourse.courseName
                this.dataForm.direction = data.tCourse.direction
                this.dataForm.courseLevel = data.tCourse.courseLevel
                this.dataForm.courseFree = data.tCourse.courseFree
                this.dataForm.coursePrice = data.tCourse.coursePrice
                this.dataForm.remark = data.tCourse.remark
                this.dataForm.studyCount = data.tCourse.studyCount
                this.dataForm.createUser = data.tCourse.createUser
                this.dataForm.createTime = data.tCourse.createTime
                this.dataForm.updateUser = data.tCourse.updateUser
                this.dataForm.updateTime = data.tCourse.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/teach/course/${!this.dataForm.courseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'courseId': this.dataForm.courseId || undefined,
                'teacherId': this.dataForm.teacherId,
                'proStageId': this.dataForm.proStageId,
                'proStageName': this.dataForm.proStageName,
                'courseName': this.dataForm.courseName,
                'direction': this.dataForm.direction,
                'courseLevel': this.dataForm.courseLevel,
                'courseFree': this.dataForm.courseFree,
                'coursePrice': this.dataForm.coursePrice,
                'remark': this.dataForm.remark,
                'studyCount': this.dataForm.studyCount,
                'createUser': this.dataForm.createUser,
                'createTime': this.dataForm.createTime,
                'updateUser': this.dataForm.updateUser,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
